//
//  NCalendarDate.swift
//  makeCalader
//
//  Created by ning on 2018/1/10.
//  Copyright © 2018年 ning. All rights reserved.
//

import Foundation

class NCalendarDate {
    
}

extension NCalendarDate {
    //获取当前日
   static func getDay(date: Date) -> Int {
        let com = (Calendar.current as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day], from: date)
        return com.day ?? 0
    }
    
    //获取当前月
    static func getMonth(date: Date) -> Int {
         let com = (Calendar.current as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day], from: date)
        return com.month ?? 0
    }
    
    //获取当前年
    static func getYear(date: Date) -> Int {
         let com = (Calendar.current as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day], from: date)
        return com.year ?? 0
    }
    
    //每个月1号对应的星期
    static func getFirstWeekInThisMonth(date: Date) -> Int {
        var calender = Calendar.current
        calender.firstWeekday = 1
        var com = (calender as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day], from: date)
        com.day = 1
        let firstDay = calender.date(from: com)
        let firstWeek = (calender as NSCalendar).ordinality(of: NSCalendar.Unit.weekday, in: NSCalendar.Unit.weekOfMonth, for: firstDay ?? Date())
        return firstWeek - 1
    }
    
    //获取当前月天数
    static func getDaysInThisMonth(date: Date) -> Int {
        let days: NSRange = (Calendar.current as NSCalendar).range(of: NSCalendar.Unit.day, in: NSCalendar.Unit.month, for: date)
        return days.length
    }
    
    
    //获取上个月天数
    static func getDaysInLastMonth(date: Date) -> Int {
        let lastDate = self.getLastMonth(date: date)
        let days: NSRange = (Calendar.current as NSCalendar).range(of: NSCalendar.Unit.day, in: NSCalendar.Unit.month, for: lastDate)
        return days.length
    }
    
    //获取上个月
    static func getLastMonth(date: Date) -> Date {
        var dateCom = DateComponents()
        dateCom.month = -1
        let newDate = (Calendar.current as NSCalendar).date(byAdding: dateCom, to: date, options: NSCalendar.Options.matchStrictly)
        return newDate ?? Date()
    }
    
    //获取下个月
    static func getNextMonth(date: Date) -> Date {
        var dateCom = DateComponents()
        let abc = 1
        dateCom.month = +abc
        let newDate = (Calendar.current as NSCalendar).date(byAdding: dateCom, to: date, options: NSCalendar.Options.matchStrictly)
        return newDate ?? Date()
    }
    
    //获取本月指定某一天的date
    static func getSomeDayDate(day: Int,date: Date) -> Date {
        let calendar = (Calendar.current as NSCalendar)
        var com = calendar.components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day], from: date)
        com.day = day
        return calendar.date(from: com) ?? Date()
    }

    //获取某一天是周几
    static func getDayOfWeek(date: Date) -> Int {
        let interval = date.timeIntervalSince1970
        let days = Int(interval / 86400)
        return (days - 3) % 7
    }
    
    //比较两个日期大小(此处比较年月日)
    static func compareDateAndAnother(dateA: Date, dateB: Date) -> Int{
        var date1 = dateA
        let com1 = (Calendar.current as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day], from: date1)
         date1 = (Calendar.current as NSCalendar).date(from: com1 ) ?? Date()
        
        var date2 = dateB
        let com2 = (Calendar.current as NSCalendar).components([NSCalendar.Unit.year, NSCalendar.Unit.month, NSCalendar.Unit.day], from: date2)
        date2 = (Calendar.current as NSCalendar).date(from: com2 ) ?? Date()
        
        if date1 < date2 {
            return 1
        }else if date1 > date2 {
            return -1
        }else{
            return 0
        }
    }
}
